const jwt = require('jsonwebtoken');
const {private_key,} = require('../app/config');

class authController {
  async login(ctx, next) { //ctx is the context of the request
//
    console.log('验证登录');
    const {id, name} = ctx.user  //ctx.user is the user object that is set in the middleware
    console.log(id, name);
    const token = jwt.sign({id, name}, private_key, {  //sign the token
      expiresIn: '10h',
      algorithm: 'RS256',
    });
    ctx.body = {
      message: `${ctx.request.body.name}登录成功`,
      status: 200,
      token,
      id: ctx.request.body.id,
      name,
    }
  }
  async test(ctx, next) {
    ctx.body = {
      message: '授权成功',
      status: 200,
    }
  }
}

module.exports = new authController();